<?php

namespace App\Transformers;

use App\Models\Prize;
use League\Fractal\TransformerAbstract;

class PrizeTransformer extends TransformerAbstract
{
    protected $availableIncludes = ['product', 'PrizeRecords'];

    public function transform(Prize $prize)
    {
        return [
            'id' => $prize->id,
            'product_id' => (int) $prize->product_id,
            'status' => (int)$prize->status,
            'quantity' => (int)$prize->quantity,
            'remain_quantity' => (int)$prize->remain_quantity,
            'redeeming_quantity' => (int)$prize->redeeming_quantity,
            'redeemed_quantity' => (int)$prize->redeemed_quantity,
            'user_redeeming_quantity' => (int)$prize->user_redeeming_quantity,
            'accumulate_point' => (int)$prize->accumulate_point,
            'shelves_begin' => $prize->shelves_begin,
            'shelves_end' => $prize->shelves_end,
            'redeeming_begin' => $prize->redeeming_begin,
            'redeeming_end' => $prize->redeeming_end,
            'redeemed_begin' => $prize->redeemed_begin,
            'redeemed_end' => $prize->redeemed_end,
            'order_num' => (int)$prize->order_num,
            'created_at' => $prize->created_at->toDateTimeString(),
            'updated_at' => $prize->updated_at->toDateTimeString(),
        ];
    }

    public function includeProduct(Prize $prize)
    {
        return $this->item($prize->product, new ProductTransformer());
    }

    public function includePrizeRecords(Prize $prize)
    {
        $prizeRecords = $prize->prizeRecords;

        return $this->collection($prizeRecords, new PrizeRecordTransformer());
    }

}